﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLab.Model
{
    public class CommandHistoryModel
    {
        private ObservableList<string> _commands;

        public delegate void CommandAddedEventHandler(object sender, CommandEventArgs e);
        public delegate void CommandRemovedEventHandler(object sender, CommandEventArgs e);
        public delegate void CommandsClearedEventHandler(object sender, EventArgs e);
        /// <summary>
        /// Raised when command is added to command history
        /// </summary>
        public event CommandAddedEventHandler CommandAdded;
        /// <summary>
        /// Raised when command is removed from command history
        /// </summary>
        public event CommandRemovedEventHandler CommandRemoved;
        /// <summary>
        /// Raised when commands are cleared
        /// </summary>
        public event CommandsClearedEventHandler CommandsCleared;

        public CommandHistoryModel()
        {
            _commands = new ObservableList<string>();
            _commands.ItemAdded += new ObservableList<string>.ItemAddedEventHandler(OnItemAdded);
            _commands.ItemRemoved += new ObservableList<string>.ItemRemovedEventHandler(OnItemRemoved);
            _commands.ListCleared += new ObservableList<string>.ListClearedEventHandler(OnListCleared);
        }

        /// <summary>
        /// Adds a command to command history
        /// </summary>
        /// <param name="Command">Command to be stored</param>
        public void AddCommand(string Command)
        {
            if (Command == string.Empty)
                return;
            if ((_commands.Count != 0) && (String.Compare(Command, _commands[_commands.Count - 1]) == 0))
                return;


            _commands.Add(Command);
        }

        /// <summary>
        /// Removes command from command history
        /// </summary>
        /// <param name="Index">Index of command to be removed</param>
        /// <returns>Command that is removed</returns>
        public string RemoveCommand(int Index)
        {
            if (Index < 0 || Index >= _commands.Count)
                return null;

            string toRemove = _commands[Index];
            _commands.RemoveAt(Index);
            return toRemove;
        }

        /// <summary>
        /// Clears commands from command history
        /// </summary>
        public void ClearCommands()
        {
            _commands.Clear();
        }

        /// <summary>
        /// Creates iterator for iteration through commands in command history
        /// </summary>
        /// <returns>Instance of iterator class</returns>
        public CommandIterator CreateIterator()
        {
            return new CommandIterator(_commands);
        }

        void OnListCleared(object source, EventArgs e)
        {
            if (CommandsCleared != null)
                CommandsCleared(this, e);
        }

        void OnItemRemoved(object source, ObservableList<string>.ListChangedEventArgs e)
        {
            if (CommandRemoved != null)
                CommandRemoved(this, new CommandEventArgs(e.item, e.index));
        }

        void OnItemAdded(object source, ObservableList<string>.ListChangedEventArgs e)
        {
            if (CommandAdded != null)
                CommandAdded(this, new CommandEventArgs(e.item, e.index));
        }
    }
}
